/**
 * Copyright 2015 Qiyi Inc. All rights reserved.
 * 
 * @file:   Album.js
 * @path:   js-src/album/
 * @author: jiegang@qiyi.com
 * @date:   2015-01-11
 */

qui(
    function(require, exports, module) {
        window.Ancestor = window.Ancestor || {
            version: '1.0'
        };
        window.SystemDialog = (function () {
            var doHandler = null;
            var _initAlert = false;
            var _initConfirm = false;

            function initAlertDlg() {
                if (!_initAlert) {
                    if ($("#epg_alert_dlg").length == 0) {
                        $('body').append('<div id="epg_alert_dlg"><div sign="detail"></div></div>');
                    }
                    $("#epg_alert_dlg").dialog({
                        title: "epg系统提醒",
                        autoOpen: false,
                        width: 400,
                        modal: true,
                        dialogClass: 'alert',
                        resizable: false,
                        closeOnEscape:false,
                        buttons: [
                            {
                                text: "确认",
                                click: function () {
                                    $(this).dialog("close");
                                }
                            }
                        ]
                    })
                    _initAlert = true;
                }
                console.log("_initAlert:" + _initAlert);
            }

            function initConfirmDlg() {
                if (!_initConfirm) {
                    if ($("#epg_confirm_dlg").length == 0) {
                        $('body').append('<div id="epg_confirm_dlg"><div sign="detail"></div></div>');
                    }
                    $("#epg_confirm_dlg").dialog({
                        title: "epg系统确认",
                        autoOpen: false,
                        width: 400,
                        modal: true,
                        dialogClass: 'alert',
                        resizable: false,
                        closeOnEscape:false,
                        buttons: [
                            {
                                text: "确认",
                                click: function () {
                                    $(this).dialog("close");
                                    doHandler(true);
                                }
                            },
                            {
                                text: "取消",
                                click: function () {
                                    $(this).dialog("close");
                                    doHandler(false);
                                }
                            }
                        ]
                    });
                    $("div[aria-describedby=epg_confirm_dlg]").find("button:eq(1)").addClass("btn btn-primary");
                    $("div[aria-describedby=epg_confirm_dlg]").find("button:eq(2)").addClass("btn");
                    _initConfirm = true;
                }
                console.log("_initConfirm:" + _initConfirm);
            }

            function queryAlert(msg) {
                initAlertDlg()
                $("#epg_alert_dlg").find("div[sign=detail]").html(msg);
                $("#epg_alert_dlg").dialog("open");
            }

            function queryConfirm(msg, callback,btnNameObj) {
                initConfirmDlg();
                $("#epg_confirm_dlg").find("div[sign=detail]").html(msg);
                var btnExtend = $.extend(true,{"btn_ok":"确认","btn_cancel":"取消"},btnNameObj);
                if( "btn_ok" in btnExtend){
                    $("div[aria-describedby=epg_confirm_dlg]").find("button:eq(1)").html(btnExtend["btn_ok"])
                }
                if( "btn_cancel" in btnExtend){
                    $("div[aria-describedby=epg_confirm_dlg]").find("button:eq(2)").html(btnExtend["btn_cancel"])
                }
                $("#epg_confirm_dlg").dialog("open");
                doHandler = function (flag) {
                    if (typeof(callback) == "function") {
                        callback(flag);
                    }
                }
            }

            return {
                queryAlert: queryAlert,
                queryConfirm: queryConfirm
            }
        })();

    }
);